-
Notifications
You must be signed in to change notification settings - Fork 51
Add support for true random number generator (RNG) peripheral #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Adds `RngExt` trait to constrain and enable RNG peripheral with HSI48 clock - Implements state machine with `Rng<Stopped>` and `Rng<Running>` types - Provides blocking and non-blocking read methods for 32-bit random values - Implements `TryRngCore` from `rand` for interoperability with rand traits - Includes error handling for seed and clock error conditions
use crate::{rcc::Rcc, stm32::RNG}; | ||
use core::{fmt::Formatter, marker::PhantomData}; | ||
|
||
pub enum RngError { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind implementing defmt::Format
when the defmt
feature is enabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a conditional impl of defmt::Format
let status = unsafe { (*RNG::ptr()).sr().read() }; | ||
|
||
// Check if the seed or clock error bits are set | ||
if status.seis().bit_is_set() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know about G4, but I believe for the H5 you have to reset the bit to ensure the peripheral is restored to a valid state. See stm32-rs/stm32h5xx-hal#37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a seed_error_recovery()
method that performs the recovery sequence from a seed error. The clock error resets itself when the clock condition (fRNGCLOCK > fHCLK/32) is satisfied per RM0440.
This requires the caller calls seed_error_recovery()
if a read returns RngError::SeedError
. I considered making reads self-recover - but I figured in some cases it would be useful to know there was a seed error so I've left it explicit.
Cargo.toml
Outdated
@@ -20,6 +20,7 @@ stm32-usbd = { version = "0.7.0", optional = true } | |||
fixed = { version = "1.28.0", optional = true } | |||
embedded-io = "0.6" | |||
stm32-hrtim = { version = "0.1.0", optional = true } | |||
rand = { version = "0.9", default-features = false } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use rand_core instead of rand?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed to rand_core
in dependencies
, and moved rand
to dev-dependencies
which is required for the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thanks
….3 RNDATA description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, looks good to me!
RngExt
trait to constrain and enable RNG peripheral with HSI48 clockRng<Stopped>
andRng<Running>
typesTryRngCore
fromrand
for interoperability with rand traits